package com.doudou.collection.domain.service.impl;


import com.doudou.collection.dao.mapper.AnnouncementMapper;
import com.doudou.collection.domain.service.AnnouncementService;
import com.doudou.collection.infrastructure.common.ex.ServiceException;
import com.doudou.collection.infrastructure.common.web.ResultVO;
import com.doudou.collection.infrastructure.common.web.StatusCode;
import com.doudou.collection.orm.dto.AnnouncementAddNewDTO;
import com.doudou.collection.orm.dto.AnnouncementUpdateDTO;
import com.doudou.collection.orm.entity.Announcement;
import com.doudou.collection.orm.vo.AnnouncementEditVO;
import com.doudou.collection.orm.vo.AnnouncementVO;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AnnouncementServiceImpl implements AnnouncementService {


    @Autowired
    private AnnouncementMapper announcementMapper;

    /**
     * 发布公告业务功能
     * @param announcementAddNewDTO
     * @return
     */
    @Override
    public void addNew(AnnouncementAddNewDTO announcementAddNewDTO) {

        log.debug("开始处理【发布公告】业务: {}", announcementAddNewDTO);

        Announcement announcement = new Announcement();
        BeanUtils.copyProperties(announcementAddNewDTO, announcement);
        int rows = announcementMapper.insert(announcement);
        if (rows != 1) {
            String message = "发布公告失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_INSERT.getCode(), message);
        }

    }


    /**
     * 修改公告为删除状态功能
     * @param id
     */
    @Override
    public void delete(Long id) {
        log.debug("开始处理【修改删除状态】业务：公告ID:{}", id);
        updateDelFlagById(id, 1);
    }

    /**
     * 修改公告为未删除状态功能
     * @param id
     */
    @Override
    public void undelete(Long id) {
        log.debug("开始处理【修改未删除状态】业务：公告ID:{}", id);
        updateDelFlagById(id, 0);
    }

    /**
     * 编辑公告业务功能
     * @param announcementUpdateDTO
     * @return
     */
    @Override
    public void updateById(AnnouncementUpdateDTO announcementUpdateDTO) {
        log.debug("开始处理【修改标签内容】业务： 修改内容：{}", announcementUpdateDTO);

        Announcement announcement = new Announcement();
        BeanUtils.copyProperties(announcementUpdateDTO, announcement);

        int rows =  announcementMapper.updateAnnouncement(announcement);
        if (rows != 1) {
            String message = "修改公告失败，服务器忙，请稍后再试!";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_UPDATE.getCode(), message);
        }
    }


    /**
     * 修改公告为撤销状态功能
     * @param id
     */
    @Override
    public void cancel(Long id) {
        log.debug("开始处理【修改撤销状态】业务：公告ID:{}", id);
        updateIfCancelById(id, 1);
    }

    /**
     * 修改公告为未撤销状态功能
     * @param id
     */
    @Override
    public void unCancel(Long id) {
        log.debug("开始处理【修改未撤销状态】业务：公告ID:{}", id);
        updateIfCancelById(id, 0);
    }

    /**
     * 查询所有公告业务功能
     * @return
     */
    @Override
    public List<AnnouncementVO> selectAll() {
        log.debug("开始处理【查询所有公告】业务");
        List<AnnouncementVO> list = announcementMapper.selectAll();

        log.info("查询成功: {}", list);
        return list;
    }

    /**
     * 修改删除状态私有方法
     * @param id  公告id
     * @param delFlag  公告删除状态
     */
    private void updateDelFlagById(Long id, Integer delFlag) {
        AnnouncementVO announcementVO = announcementMapper.getAnnouncementById(id);
        if (announcementVO.getDelFlag() == delFlag) {
            String message = "修改失败，该公告已此状态";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_UPDATE.getCode(), message);
        }

        Announcement announcement = new Announcement();
        announcement.setId(id);
        announcement.setDelFlag(delFlag);
        announcement.setDeleteTime(new Date());
        int rows = announcementMapper.updateDelFlag(announcement);
        if (rows != 1) {
            String message = "修改状态失败，服务器忙，请稍后再试!";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_UPDATE.getCode(), message);
        }

    }

    /**
     * 修改撤回状态私有方法
     * @param id  公告id
     * @param ifCancel  公告删除状态
     */
    private void updateIfCancelById(Long id, Integer ifCancel) {
        AnnouncementVO announcementVO = announcementMapper.getAnnouncementById(id);

        if (announcementVO.getIfCancel() == ifCancel) {
            String message = "修改失败，该公告已此状态";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_UPDATE.getCode(), message);
        }

        Announcement announcement = new Announcement();
        announcement.setId(id);
        announcement.setIfCancel(ifCancel);
        announcement.setCancelTime(new Date());

        int rows = announcementMapper.updateIfCancel(announcement);
        if (rows != 1) {
            String message = "修改状态失败，服务器忙，请稍后再试!";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_UPDATE.getCode(), message);
        }
    }

}
